/**
 * 
 */
package at.fhj.ase.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ASE10 - SS2011 
 * Project work Client Architecture and Design
 *  
 * @author Henning Diepold
 * @author Dieter Steiner
 */

public class CustomerDAOImpl implements CustomerDAO{

	private final String JDBC_CONNECT_STRING = "jdbc:mysql://localhost:3306/tinybank";
	private final String USERNAME = "test";
	private final String PASSWORD = "test";
	private final Pattern NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9 -\\.^'_]+$");
	
	@Override
	public void insert(Customer customer) {
		// TODO Auto-generated method stub
	}

	@Override
	public void update(Customer customer) {
		// TODO Auto-generated method stub
	}

	@Override
	public void delete(Customer customer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Customer findById(int pKey) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Customer findByUsername(String username) throws ClassNotFoundException {
		if(null == username) {
			throw new NullPointerException("username: not null!");
		}
		else if("" == username) {
			throw new IllegalArgumentException("username: not empty!");
		}
		else if(!checkNamePattern(username)) {
			throw new IllegalArgumentException("username: characters not allowed!");
		}
		
		final String SQL = "select id, firstname, lastname, username, email, postcode, location, street, country from customers where username = ?";
		
		Class.forName("com.mysql.jdbc.Driver");
		
		try{
			Connection connection = DriverManager.getConnection(JDBC_CONNECT_STRING, USERNAME, PASSWORD);
			PreparedStatement ps = connection.prepareStatement(SQL);
			ps.setString(1,username);
			ResultSet rs = ps.executeQuery();
			rs.next();
			return new Customer(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt(6), rs.getString(7), rs.getString(8), rs.getString(9));
		}
		catch(SQLException sqlEx) {
			System.err.println(sqlEx.getMessage());
			return null;
		}

	}

	@Override
	public ArrayList<Customer> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Account> findAccounts(int customerId) throws ClassNotFoundException {
		if(null == (Integer)customerId) {
			throw new NullPointerException("customerId: not null!");
		}

		final String SQL = "select id, sum_amount from accounts where customers_id = ?";
		Class.forName("com.mysql.jdbc.Driver");
		
		try{
			Connection connection = DriverManager.getConnection(JDBC_CONNECT_STRING, USERNAME, PASSWORD);
			PreparedStatement ps = connection.prepareStatement(SQL);
			ps.setInt(1, customerId);
			ResultSet rs = ps.executeQuery();
			ArrayList<Account> accounts = new ArrayList<Account>();
			while (rs.next()) {
				System.out.println("> Accounts for customerID " + customerId );
				System.out.println("\t> customerId: " + rs.getInt(1) + ", sum_amount: " + rs.getLong(2));
				Account account = new Account();
				account.setId(rs.getInt(1));
				account.setSumAmount(rs.getLong(2));
				accounts.add(account);
			}
			return accounts;
		}
		catch(SQLException sqlEx){
			System.err.println(sqlEx.getMessage());
			return null;
		}
		
	}
	
    /**
     * @param name
     * @return
     */
    private boolean checkNamePattern(String name) {
    	Matcher m = NAME_PATTERN.matcher(name);
    	Boolean b = m.matches();
    	return b;
    }

}
